package com.adinnet.api.classs.service.impl;

import com.adinnet.api.child.mapper.ChildMapper;
import com.adinnet.api.child.model.Child;
import com.adinnet.api.childMaxStudy.mapper.ChildMaxStudyMapper;
import com.adinnet.api.childMaxStudy.model.ChildMaxStudy;
import com.adinnet.api.childStudyRecord.mapper.ChildStudyRecordMapper;
import com.adinnet.api.childStudyRecord.model.ChildStudyRecord;
import com.adinnet.api.classs.mapper.ClasssMapper;
import com.adinnet.api.classs.model.Classs;
import com.adinnet.api.classs.model.vo.ClasssResp;
import com.adinnet.api.classs.service.ClasssService;
import com.adinnet.api.sentence.mapper.SentenceMapper;
import com.adinnet.api.sentence.model.Sentence;
import com.adinnet.api.video.mapper.VideoMapper;
import com.adinnet.api.video.model.Video;
import com.adinnet.api.word.mapper.WordMapper;
import com.adinnet.api.word.model.Word;
import com.adinnet.common.utils.DateUtil;
import com.adinnet.core.JsonResult;
import com.adinnet.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 班级名称 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@Service
public class ClasssServiceImpl extends ServiceImpl<ClasssMapper, Classs> implements ClasssService {


    @Autowired
    private ClasssMapper classsMapper;

    @Autowired
    private SentenceMapper sentenceMapper;

    @Autowired
    private WordMapper wordMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private ChildMaxStudyMapper childMaxStudyMapper;

    @Autowired
    private ChildStudyRecordMapper childStudyRecordMapper;

    @Autowired
    private ChildMapper childMapper;

    /**
     * 查询班级课程列表
     *
     * @param id
     */
    @Override
    public List<ClasssResp> classsList(Integer id, Integer childId) {
        Child child = childMapper.selectById(childId);
        if (null == child) {
            throw new BizException("未查询到儿童信息!");
        }
        String currentDate = DateUtil.getCurrentDate();//当前日期
        Date childLastStudyDate = child.getLastStudyDate();
        if (null == childLastStudyDate) {
            child.setLastStudyDate(new Date());
            child.setSumStudyDays(1);
        } else if (new SimpleDateFormat("yyyy-MM-dd").format(childLastStudyDate).equals(currentDate)) {

        } else if (!new SimpleDateFormat("yyyy-MM-dd").format(childLastStudyDate).equals(currentDate)) {
            child.setLastStudyDate(new Date());
            child.setSumStudyDays(child.getSumStudyDays() + 1);
        }
        childMapper.updateById(child);
        return classsMapper.classsList(id);
    }


    /**
     * 查看课程列表
     *
     * @param courseId
     * @param type
     * @return
     */
    @Override
    public JsonResult classsListDetails(Integer childId, Integer courseId, String type) {
        if ("SENTENCE".equals(type)) {
            //跟读联系
            QueryWrapper<Sentence> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id", courseId).eq("deleted", 0).orderByAsc("serial_number");
            List<Sentence> sentences = sentenceMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(sentences)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            //2021-03-01 单词短语随机排序
            Collections.shuffle(sentences);
            //历史高分和历史录音(最新一条)
            for (Sentence sentence : sentences) {
                QueryWrapper<ChildMaxStudy> childWrapper = new QueryWrapper<>();
                childWrapper.eq("child_id", childId).eq("type", 4).eq("third_id", sentence.getId()).eq("deleted", 0).last("limit 1");
                ChildMaxStudy childMaxStudy = childMaxStudyMapper.selectOne(childWrapper);
                if (null == childMaxStudy) {
                    sentence.setHistoryScore(0);
                } else {
                    sentence.setHistoryScore(childMaxStudy.getPeaseNum());
                }

                QueryWrapper<ChildStudyRecord> studyWrapper = new QueryWrapper<>();
                studyWrapper.eq("child_id", childId).eq("type", 4).eq("third_id", sentence.getId()).eq("deleted", 0).orderByDesc("created_date").last("limit 1");
                List<ChildStudyRecord> studyRecords = childStudyRecordMapper.selectList(studyWrapper);
                if (CollectionUtils.isEmpty(studyRecords)) {
                    sentence.setHistoryReadUrl("");
                    sentence.setHistoryReadErrorWords("");
                } else {
                    sentence.setHistoryReadUrl(studyRecords.get(0).getReadUrl() == null ? "" : studyRecords.get(0).getReadUrl());
                    sentence.setHistoryReadErrorWords(studyRecords.get(0).getErrorWords() == null ? "" : studyRecords.get(0).getErrorWords());
                }
            }
            return JsonResult.ok().put("data", sentences);
        }
        if ("WORD".equals(type)) {
            //重点词汇
            QueryWrapper<Word> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id", courseId).eq("deleted", 0).orderByAsc("serial_number");
            List<Word> words = wordMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(words)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            //2021-03-01 单词短语随机排序
            Collections.shuffle(words);
            //历史高分
            for (Word word : words) {
                QueryWrapper<ChildMaxStudy> childWrapper = new QueryWrapper<>();
                childWrapper.eq("child_id", childId).eq("type", 3).eq("third_id", word.getId()).eq("deleted", 0).last("limit 1");
                ChildMaxStudy childMaxStudy = childMaxStudyMapper.selectOne(childWrapper);
                if (null == childMaxStudy) {
                    word.setHistoryScore(0);
                } else {
                    word.setHistoryScore(childMaxStudy.getPeaseNum());
                }
            }
            return JsonResult.ok().put("data", words);
        }
        if ("VOICE_VIDEO".equals(type) || "VOICE".equals(type)) {
            //视频|音频
            QueryWrapper<Video> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id", courseId).eq("deleted", 0).orderByAsc("serial_number");
            if ("VOICE_VIDEO".equals(type)) wrapper.eq("type", type);
            else wrapper.apply("FIND_IN_SET(type,'VOICE_VIDEO,VOICE')");
            // wrapper.eq("type",type);
            List<Video> videos = videoMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(videos)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            return JsonResult.ok().put("data", videos);
        }
        return JsonResult.error("请选择正确的课程类型!");
    }
}
